# -*- coding: utf-8 -*-
import sys
sys.path.append("../../quidditas")

import pygame

import quidditas
from pygame.time import Clock
from pygame.image import load

import type_definitions, processors, components

def start():
    pygame.init()
    pygame.display.set_caption("Monkey fever")
    pygame.mouse.set_visible(0)

    screen = pygame.display.set_mode((468, 60))
    clock = Clock()

    sounds = {"punch": load_sound("res/punch.wav"),
              "whiff": load_sound("res/whiff.wav")
              }

    q = quidditas.Quidditas()
    q.add_type_definitions(type_definitions.get_definitions())
    fist_proc = q.add_processor(processors.FistControlProcessor())
    punch_proc = q.add_processor(processors.PunchProcessor(sounds))
    chimp_proc = q.add_processor(processors.ChimpMovementProcessor())
    text_proc = q.add_processor(processors.DrawTextProcessor(screen))
    render_proc = q.add_processor(processors.RenderProcessor(screen))

    q.create_entity("chimp")
    q.create_entity("fist")

    running = 1
    while running:
        for event in pygame.event.get(pygame.QUIT):
            running = 0
        time = clock.get_time()
        q.update(time)
        fist_proc.update(time)
        punch_proc.update(time)
        chimp_proc.update(time)
        text_proc.update(time)
        render_proc.update(time)
        
        pygame.display.flip()

        clock.tick(60)
    pygame.quit()
    sys.exit()


def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer:
        return NoneSound()
    try:
        sound = pygame.mixer.Sound(name)
    except pygame.error, message:
        print 'Cannot load sound:', wav
        raise SystemExit, message
    return sound

if __name__ == "__main__":
    start()
